import { defineStore } from "pinia";
import { OPEN_DYNAMIC_ROUTER } from "../config/config";
import router from "../router";
import { Item } from "ant-design-vue/es/menu";

/**
 * @description 动态路由参数配置简介
 * @param path ==> 菜单路径
 * @param name ==> 菜单别名
 * @param redirect ==> 重定向地址
 * @param component ==> 视图文件路径
 * @param meta ==> 菜单信息
 * @param meta.icon ==> 菜单图标
 * @param title ==> 菜单标题
 * @param meta.activeMenu ==> 当前路由为详情页时，需要高亮的菜单
 * @param meta.isLink ==> 是否外链
 * @param meta.isHide ==> 是否隐藏
 * @param meta.isFull ==> 是否全屏(示例：数据大屏页面)
 * @param meta.isAffix ==> 是否固定在 tabs nav
 * @param meta.isKeepAlive ==> 是否缓存
 * */

export const useAuthMenu = defineStore("authMenu", {
  state: () => ({
    // 当前页面的 router name，用来做按钮权限筛选
    activeMenuPath: "",
    //当前路由面包屑
    currentBreadcrumb: [],
    //tab栏列表
    tabList: [
      { name: "首页", path: "/home", icon: "HomeOutlined", isAffix: true },
    ],
    // 按钮权限列表
    authButtonList: [],
    // 静态菜单列表
    staticMenuList: undefined,
    // 动态菜单列表
    dynamicMenuList: undefined,
    //菜单列表
    authMenuList: [],
  }),

  getters: {},
  actions: {
    updateBreadcrumb(key) {
      const breadcrumbList = [];
      this.findByPath(key, this.authMenuList, breadcrumbList);
      this.currentBreadcrumb = breadcrumbList;
    },

    findByPath(routerPath, menuList, breadcrumbList) {
      if (!menuList || menuList.length === 0) {
        return false;
      }
      for (let i = 0; i < menuList.length; i++) {
        const menu = menuList[i];
        if (menu.key === routerPath) {
          breadcrumbList.push(menu);
          return true;
        }
        const children = menu.children;
        const result = this.findByPath(routerPath, children, breadcrumbList);
        if (result) {
          breadcrumbList.unshift(menu);
          return true;
        }
      }
      return false;
    },
  },
});
